package com.aizhixin.lab.course.classes.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.service.SystemSetService;
import com.aizhixin.lab.course.classes.domain.CourseClassDomain;
import com.aizhixin.lab.course.classes.domain.QueryCourseClassDomain;
import com.aizhixin.lab.course.classes.dto.CourseClassDTO;
import com.aizhixin.lab.course.classes.service.CourseClassService;
import com.aizhixin.lab.course.classes.service.CourseClassStuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/class")
@Api(value = "课程班级相关API")
public class CourseClassController {
	@Autowired
	private AccountService accountService;
	@Autowired
	private CourseClassService courseClassService;
	@Autowired
	private CourseClassStuService courseClassStuService;

	
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "课程添加班级或学生", response = Void.class, notes = "课程添加班级或学生<br>@author zhengning")
	public ResponseEntity<?> saveInfor(@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id必填,选中一个班级对应一个CourseClassDomain实体，若是按学生选择则把学生id集合放入userIds（选修班选学生会清空之前的学生列表，所以需要把当前需要加入选修班的学生id都放入userIds）", required = false) @RequestBody List<CourseClassDomain> domainList) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		
		Map<String, Object> result = courseClassService.save(domainList,account.getId());
		return new ResponseEntity(result, HttpStatus.OK);
	}


	@RequestMapping(value = "/page", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "按课程名称分页查询班课信息", response = Void.class, notes = "按课程名称分页查询班课信息<br>@author zhengning")
	public ResponseEntity<?> getCourseInfoPage(@RequestHeader("Authorization") String token,
			@RequestBody QueryCourseClassDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		Map<String, Object> result = courseClassService.query(PageJdbcUtil.createNoErrorPageRequest(domain.getPageNumber(), domain.getPageSize()), domain.getName(),account.getId(),domain.getCourseId());
		return new ResponseEntity(result, HttpStatus.OK);
	}

	
	@RequestMapping(value = "/findone", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "按课程id查询班课信息", response = Void.class, notes = "按课程id查询班课信息<br>@author zhengning")
	public ResponseEntity<?> findOne(@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		Map<String, Object> result =  new HashMap<String, Object>();
		List<CourseClassDTO> list =	courseClassService.findCourseClassList("'"+courseId+"'");
		result.put(ApiReturnConstants.DATA, list);
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/findCourseClass", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "按课程id查询改班课下的班级", response = Void.class, notes = "按课程id查询改班课下的班级<br>@author lwq")
	public ResponseEntity<?> findCourseClass(@RequestHeader("Authorization") String token,
									 @ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = false) String courseId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result =  new HashMap<String, Object>();
		List<CourseClassDTO> list =	courseClassService.findMyCourseClassList(courseId,account.getId());
		result.put(ApiReturnConstants.DATA, list);
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/stupage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "查询选修课学生列表", response = Void.class, notes = "查询选修课学生列表<br>@author zhengning")
	public ResponseEntity<?> stuPage(@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseClassId课程选修班id(必填)stuName学生姓名/学号(选填)", required = false) @RequestBody QueryCourseClassDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		Map<String, Object> result = null;
		if(StringUtils.isEmpty(domain.getCourseClassId())){
			result.put(ApiReturnConstants.ERROR, "班课id不能为空！");
			result.put(ApiReturnConstants.SUCCESS, false);
			return new ResponseEntity(result, HttpStatus.OK);
		}
		result = courseClassStuService.findStudentPage(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "DELETE", value = "删除课程班级", response = Void.class, notes = "删除课程班级<br>@author zhengning")
	public ResponseEntity<?> deleteClass(@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseClassId课程班级id", required = false) @RequestParam(value = "courseClassId", required = true) String courseClassId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		
		Map<String, Object> result = courseClassService.delete(courseClassId);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/initstu", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "初始化班课的行政班学生信息（仅供历史测试数据使用）", response = Void.class, notes = "初始化班课的行政班学生信息（仅供历史测试数据使用）<br>@author zhengning")
	public void initStu() {
		courseClassService.initClassStu();
	}
	
	
	@RequestMapping(value = "/classmate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "按课程id查询同班同学", response = Void.class, notes = "按课程id查询同班同学<br>@author zhengning")
	public ResponseEntity<?> classmate(@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		Map<String, Object> result =  new HashMap<String, Object>();
		List<CourseClassDTO> list =	courseClassService.findCourseClassList("'"+courseId+"'");
		result.put(ApiReturnConstants.DATA, list);
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}


}
